﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Database;
using System.Collections.Generic;
using Database.Classes;

namespace smallDeimos.Game
{
    public partial class Planet : System.Web.UI.Page
    {
        int sId;
        int pId;
        int UserId;
        protected string CalcPop(object farms)
        {
            double l;
            if (double.TryParse(farms.ToString(), out l) == true)
            {
                double growthBonus = UserDefinedFunctions.GetBonusForGrowth(UserId);
                int f = (int)l + 1;
                if (growthBonus >= 0)
                    return "(" + f.ToString() + " +" + (int)(100 * growthBonus) + "% = " + ((int)(f * (1 + growthBonus))).ToString() + "/h)";
                else
                    return "(" + f.ToString() + " -" + (int)(100 * growthBonus) + "% = " + ((int)(f * (1 + growthBonus))).ToString() + "/h)";

            }
            return string.Empty;
        }
        protected string ShipCost(object what)
        {
            int EcoBonus = UserDefinedFunctions.EcoBonus(UserId);
            switch (what.ToString())
            {
                case "1":
                    return "(" + (int.Parse(StoredProcedures.GetConfigurationForOption("ColonyShipPrice")) - EcoBonus).ToString() + "pp)";
                case "2":
                    return "(" + (int.Parse(StoredProcedures.GetConfigurationForOption("TransportPrice")) - EcoBonus).ToString() + "pp)";
                case "3":
                    return "(" + (int.Parse(StoredProcedures.GetConfigurationForOption("DestroyerPrice")) - EcoBonus).ToString() + "pp)";
                case "4":
                    return "(" + (8 * (int.Parse(StoredProcedures.GetConfigurationForOption("DestroyerPrice")) - EcoBonus)).ToString() + "pp)";
                case "5":
                    return "(" + (20 * (int.Parse(StoredProcedures.GetConfigurationForOption("DestroyerPrice")) - EcoBonus)).ToString() + "pp)";
                default: return "";
            }
        }
        protected string BuldingCost(object level)
        {
            bool market = UserDefinedFunctions.PlayerHasSpecialBulding(UserId, SpecialBuldings.Market);
            int l;
            if (int.TryParse(level.ToString(), out l) == true)
            {
                if (l > 20)
                    return "(Use SU)";
                if (market == false)
                    return "(" + Equations.BuildingCost(l).ToString() + "pp)";
                else
                    return "(" + ((int)(Equations.BuildingCost(l) * 0.9)).ToString() + "pp)";
            }
            else
            {
                return "0";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

            //if (Page.IsPostBack == true)
            //{
            //    string[] tmp = DropDownList1.SelectedValue.Split(';');
            //   // Response.Write(DropDownList1.SelectedValue);
            //    Response.Redirect("Planet.aspx?sId=" + tmp[0] + "&pId=" + tmp[1]);
            //}

            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            try
            {
                sId = int.Parse(this.Request.QueryString["sId"]);
                pId = int.Parse(this.Request.QueryString["pId"]);
                HiddenField1.Value = pId.ToString();
                HiddenField2.Value = sId.ToString();
                int planetOwner = UserDefinedFunctions.PlanetOwner(sId, pId).Value;
                if (UserId != planetOwner)
                {
                    Response.Redirect("Planets.aspx");
                }
                if (UserDefinedFunctions.FleetOwner(sId, pId) != 0 && UserDefinedFunctions.FleetOwner(sId, pId) != UserDefinedFunctions.PlanetOwner(sId, pId))
                {
                    Label11.Text = "Planet is sieged by enemy forces";
                    DataList1.Enabled = false;
                }
                //if (UserDefinedFunctions.GetPlayerFloat(UserId, "Math").Value < 15)
                //{

                //}
                Dictionary<string, string> planets = UserDefinedFunctions.GetPlayerPlanets(UserId);
                int idx = DropDownList1.SelectedIndex;
                DropDownList1.Items.Clear();
                foreach (string k in planets.Keys)
                {
                    // Response.Write(k + " " + planets[k] + "<br/>");
                    DropDownList1.Items.Add(new ListItem(HttpUtility.HtmlEncode(planets[k]), k));
                }
                if (idx >= 0)
                    DropDownList1.SelectedIndex = idx;
                else
                    DropDownList1.SelectedValue = sId.ToString() + ";" + pId.ToString();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {//farms
            StoredProcedures.Build(sId, pId, BuldingTypes.Farm, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Factory, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Cybernet, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Lab, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Starbase, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.CS, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.CS, 10);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.CS, 50);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Transport, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button10_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Transport, 10);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button11_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Transport, 50);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button12_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Destroyer, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button13_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Destroyer, 10);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button14_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Destroyer, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button15_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Destroyer, 1000);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button16_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Cruiser, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button17_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Cruiser, 10);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button18_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Cruiser, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button19_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Cruiser, 1000);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button20_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Battleship, 1);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button21_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Battleship, 10);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button22_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Battleship, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button23_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Battleship, 1000);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Response.Write(DropDownList1.SelectedValue);
            string[] tmp = DropDownList1.SelectedItem.Value.Split(';');
            Response.Redirect("Planet.aspx?sId=" + tmp[0] + "&pId=" + tmp[1]);
        }

        protected void Button24_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Farm, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button25_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Factory, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button26_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Cybernet, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button27_Click(object sender, EventArgs e)
        {
            StoredProcedures.Build(sId, pId, BuldingTypes.Lab, 100);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button28_Click(object sender, EventArgs e)
        {
            StoredProcedures.UseSupplayUnits(sId, pId, BuldingTypes.Farm);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button29_Click(object sender, EventArgs e)
        {
            StoredProcedures.UseSupplayUnits(sId, pId, BuldingTypes.Factory);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button30_Click(object sender, EventArgs e)
        {
            StoredProcedures.UseSupplayUnits(sId, pId, BuldingTypes.Cybernet);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button31_Click(object sender, EventArgs e)
        {
            StoredProcedures.UseSupplayUnits(sId, pId, BuldingTypes.Lab);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Unnamed1_Click(object sender, EventArgs e)
        {
            string[] tmp = DropDownList1.SelectedItem.Value.Split(';');
            Response.Redirect("Planet.aspx?sId=" + tmp[0] + "&pId=" + tmp[1]);

        }

    }
}
